use crate::gen_title;

pub fn ch2_13_1() {
    gen_title("ch2_13_1", 18);

    let condition = true;
    let number = if condition { 5 } else { 6 };

    println!("The value of number is: {}", number);
}

pub fn ch2_13_2() {
    gen_title("ch2_13_2", 18);

    let n = 6;

    if n % 4 == 0 {
        println!("number is divisible by 4");
    } else if n % 3 == 0 {
        println!("number is divisible by 3");
    } else if n % 2 == 0 {
        println!("number is divisible by 2");
    } else {
        println!("number is not divisible by 4, 3, or 2");
    }
}

pub fn ch2_13_3() {
    gen_title("ch2_13_3", 18);

    for i in 1..=5 {
        println!("{}", i);
    }
}

pub fn ch2_13_4() {
    gen_title("ch2_13_4", 18);

    let a = [4, 3, 2, 1];

    for (i, v) in a.iter().enumerate() {
        println!("第{}个元素是{}", i + 1, v);
    }
}

pub fn ch2_13_5() {
    gen_title("ch2_13_5", 18);

    for i in 1..4 {
        if i == 2 {
            continue;
        }

        println!("{}", i);
    }
}

pub fn ch2_13_6() {
    gen_title("ch2_13_6", 18);

    for i in 1..4 {
        if i == 2 {
            break;
        }
        println!("{}", i);
    }
}

pub fn ch2_13_7() {
    gen_title("ch2_13_7", 18);

    let mut n = 0;

    while n <= 5 {
        println!("{}", n);

        n = n + 1;
    }

    println!("我出来了!")
}

pub fn ch2_13_8() {
    gen_title("ch2_13_8", 18);

    let mut n = 0;

    loop {
        if n > 5 {
            break;
        }
        println!("{}", n);
        n += 1;
    }

    println!("我出来了!")
}

pub fn ch2_13_9() {
    gen_title("ch2_13_9", 18);

    let a = [10, 20, 30, 40, 50];
    let mut index = 0;

    while index < 5 {
        println!("the value is: {}", a[index]);

        index = index + 1;
    }
}

pub fn ch2_13_10() {
    gen_title("ch2_13_10", 18);

    let mut counter = 0;

    let result = loop {
        counter += 1;

        if counter == 10 {
            break counter * 2;
        }
    };

    println!("The result is {}", result);
}

pub fn ch2_13_pra_1() {
    gen_title("ch2_13_pra_1", 18);

    let n = 5;

    if n < 0 {
        println!("{} is negative", n);
    } else if n > 0 {
        println!("{} is positive", n);
    } else {
        println!("{} is zero", n);
    }
}

pub fn ch2_13_pra_2() {
    gen_title("ch2_13_pra_2", 18);

    let n = 5;

    let big_n = if n < 10 && n > -10 {
        println!("数字太小, 先增加 10 倍再说");
        10 * n
    } else {
        println!("数字太大, 我们得让他减半");
        n / 2
    };

    println!("{} -> {}", n, big_n);
}

pub fn ch2_13_pra_3() {
    gen_title("ch2_13_pra_3", 18);
    for n in 1..100 {
        if n == 100 {
            println!("Never let this run");
        }
        print!("{} ", n);
    }
    println!();
}

pub fn ch2_13_pra_4() {
    gen_title("ch2_13_pra_4", 18);

    let names = ["liming".to_string(), "hanmeimei".to_string()];
    for name in &names {
        println!("{}", name);
    }

    println!("{:?}", names);

    let numbers = [1, 2, 3];
    for n in numbers {
        println!("{}", n);
    }

    println!("{:?}", numbers);
}

pub fn ch2_13_pra_5() {
    gen_title("ch2_13_pra_5", 18);

    let a = [4, 3, 2, 1];

    for (i, v) in a.iter().enumerate() {
        println!("第{}个元素是{}", i + 1, v);
    }
}

pub fn ch2_13_pra_6() {
    gen_title("ch2_13_pra_6", 18);

    let mut n = 1;

    while n < 10 {
        if n % 15 == 0 {
            println!("fizzbuzz");
        } else if n % 3 == 0 {
            println!("fizz");
        } else if n % 5 == 0 {
            println!("buzz");
        } else {
            println!("{}", n);
        }

        n += 1;
    }

    println!("n 的值是 {}, 循环结束", n);
}

pub fn ch2_13_pra_7() {
    gen_title("ch2_13_pra_7", 18);

    let mut n = 0;
    for i in 0..=100 {
        if n == 66 {
            break;
        }
        n += 1;
    }

    assert_eq!(n, 66);
    println!("n 的值是 {}, 循环结束", n);
}

pub fn ch2_13_pra_8() {
    gen_title("ch2_13_pra_8", 18);

    let mut n = 0;
    for i in 0..=100 {
        if n != 66 {
            n += 1;
            continue;
        }

        break;
    }

    assert_eq!(n, 66);
    println!("n 的值是 {}, 循环结束", n);
}

pub fn ch2_13_pra_9() {
    gen_title("ch2_13_pra_9", 18);

    let mut count = 0u32;

    println!("Let's count until infinity!");

    loop {
        count += 1;
        if count == 3 {
            println!("three");

            continue;
        }

        println!("{}", count);

        if count == 5 {
            println!("Ok, that's enough");

            break;
        }
    }

    assert_eq!(count, 5);
}

pub fn ch2_13_pra_10() {
    gen_title("ch2_13_pra_10", 18);

    let mut counter = 0;

    let result = loop {
        counter += 1;

        if counter == 10 {
            break counter * 2;
        }
    };

    assert_eq!(result, 20);
    println!("The result is {}", result);
}

pub fn ch2_13_pra_11() {
    gen_title("ch2_13_pra_11", 18);

    let mut count = 0;

    'outer: loop {
        'inner1: loop {
            if count >= 20 {
                break 'inner1;
            }
            count += 2;
        }

        count += 5;

        'inner2: loop {
            if count >= 30 {
                break 'outer;
            }

            continue 'outer;
        }
    }

    assert_eq!(count, 30);
    println!("count is {}", count);
}
